package com.libnetwork.extension

import android.net.Uri
import com.libnetwork.httpservice.HttpService

val String.apiBaseUrl: String?
    get() {
        return HttpService.instance.apiBaseUrl
    }

val String.h5BaseUrl: String?
    get() {
        return HttpService.instance.h5BaseUrl
    }

val String.absoluteApiUrl: String
    get() {
        return absoluteUrl(apiBaseUrl)
    }

val String.absoluteH5Url: String
    get() {
        return absoluteUrl(h5BaseUrl)
    }

fun String.absoluteUrl(baseUrl: String?): String {
    if (baseUrl == null) {
        return this
    }

    val uri = Uri.parse(this)

    return when {
        uri.host != null -> {
            this
        }
        firstOrNull() == '/' -> {
            "${baseUrl}${this}"
        }
        else -> {
            "${baseUrl}/${this}"
        }
    }
}